function loadUsers(str) {
    $('.card').hide();
    $('.main-content').find('#'+str).show();
    $(`.sideItem`).removeClass('active');
    $(`.sideItem[data="${str}"]`).addClass('active');


    if(str == "view-users"){
        loadRoles();
        queryAllUser();
    } else if(str == "view-tickets"){
        queryTicket();
    }
}

let queryAllUser = () => {
    $.ajax({
        url: '/listAllUser',
        type: 'POST',
        success: function(res) {
            showAllUser(JSON.parse(res))
        },
        error: function(xhr, status, error) {
            console.log('error')
        }
    });
};

let queryTicket = () =>{
    $.ajax({
        url: '/queryTicket',
        type: 'POST',
        data: {
            type: "all"
        },
        success: function (res){
            showAllTicket(JSON.parse(res));
        },
        error: function (xhr, status, error){
            console.log('error')
        }
    });
};

let showAllTicket = (tickets) =>{
    $('#ticket-list').empty();
    tickets.forEach(t => {
        $("#ticket-list").append(`<tr>
            <td>${t.ticketID}</td>
            <td>${t.lineName}</td>
            <td>${t.passby.split('-')[0]}</td>
            <td>${t.passby.split('-')[t.siteNum - 1]}</td>
            <td>${t.passby}</td>
            <td>${t.price}</td>
            <td>${t.restNumber}</td>
            <td>${t.siteNum}</td>
            <td>${t.levelName}</td>
        </tr>`);
    });
}

let showAllUser = (users => {
    $("#users-list").empty();
    users.forEach(user => {
        $("#users-list").append(`<tr>
            <td>${user.id}</td>
            <td>${user.name}</td>
            <td>${user.account}</td>
            <td>${user.email}</td>
            <td>${rolelist.find(r => r.roleID == user.role).roleName}</td>
            <td>${user.createTime}</td>
            ${
                user.id === userid ? "" :
                `<td><input type="button" onclick="delUser(e)"  value="删除" data-userid="${user.id}"/></td>`
            }
        </tr>`);
    });
    $("#viewUsersCard").show();
})
//删除用户
let delUser = (e) => {
    let userId = $(e.target).data('userid');
    $.ajax({
        url: '/deleteUser',
        type: 'POST',
        data: userId,
        success: function(res) {
             if (res == "true") {
                 alert("删除成功");
             } else {
                 alert("删除失败");
             }
        },
        error: function(xhr, status, error) {
            console.log('删除失败')
        }
    })
}


//加载权限列表
let rolelist = [];
let _setRolelist = (rl) => {
    rolelist = JSON.parse(rl);
}

let loadRoles = () => {
    $.ajax({
        url: '/getInfo',
        type: 'POST',
        data: {type:"role"},
        success: function (res){
            _setRolelist(res);
        }
    })
}

$('#creatTicket').click(()=>{
    let uid = userid;
    let data = {
        passby : $('input[id="passby"]').val(),
        price : $('input[id="price"]').val(),
        name : $('input[id="name"]').val(),
        num : $('input[id="num"]').val(),
        tktLv : $('#tktLv')[0].value
    };
    $.ajax({
        url: '/createTicket',
        type: 'POST',
        data: data,
        success: function(res) {
            console.log(res)
            if (res == "true") {
                alert("创建成功");
                loadUsers('create-ticket');
            } else {
                alert("创建失败");
            }
        },
        error: function(xhr, status, error) {
            console.log(error)
        }
    })
})

$().ready(()=>{
    loadUsers('view-users');
})